/** 
 * Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package at.socialconference.web.client.ui;

import at.socialconference.inter.model.User;
import at.socialconference.web.client.SCwebService.Util;
import at.socialconference.web.client.controller.ViewController;
import at.socialconference.web.client.util.EmptyAsyncCallback;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;

/**
 * Provides the menu of the SCweb application
 * 
 */
public class Menu extends Composite {

	private ModifiedStackPanel menuStackPanel = new ModifiedStackPanel();
	private boolean loggedIn = false;

	public Menu() {
		super();
		initWidget(menuStackPanel);
		menuStackPanel.setWidth("200px");
	}

	public void setLoggedIn(boolean loggedIn) {
		menuStackPanel.clear();
		this.loggedIn = loggedIn;
		if (loggedIn == false) {
			menuStackPanel.add(new Label(), "Login");
			menuStackPanel.hideStackContent(menuStackPanel.getWidgetCount() - 1, true);
			menuStackPanel.setCommand(menuStackPanel.getWidgetCount() - 1, MenuCommands.login);

			menuStackPanel.add(new Label(), "Register");
			menuStackPanel.hideStackContent(menuStackPanel.getWidgetCount() - 1, true);
			menuStackPanel.setCommand(menuStackPanel.getWidgetCount() - 1, MenuCommands.register);
		} else {
			menuStackPanel.add(new Label(), "Logout");
			menuStackPanel.hideStackContent(menuStackPanel.getWidgetCount() - 1, true);
			menuStackPanel.setCommand(menuStackPanel.getWidgetCount() - 1, MenuCommands.logout);

			menuStackPanel.add(new Label(), "Conferences");
			menuStackPanel.hideStackContent(menuStackPanel.getWidgetCount() - 1, true);
			menuStackPanel.setCommand(menuStackPanel.getWidgetCount() - 1, MenuCommands.home);

			menuStackPanel.add(createFriendItem(), "Friends");
			menuStackPanel.setCommand(menuStackPanel.getWidgetCount() - 1, MenuCommands.friends);
			
			User u = ViewController.getInstance().getClientUser();
			
			//admin has security level 2
			if (u != null && u.getSecurityLevel()==2) {
				menuStackPanel.add(new Label(), "Admin Area");
				menuStackPanel.hideStackContent(menuStackPanel.getWidgetCount() - 1, true);
				menuStackPanel.setCommand(menuStackPanel.getWidgetCount() - 1, MenuCommands.admin);
			}
		}

		menuStackPanel.add(new Label(), "About");
		menuStackPanel.hideStackContent(menuStackPanel.getWidgetCount() - 1, true);
		menuStackPanel.setCommand(menuStackPanel.getWidgetCount() - 1, MenuCommands.about);
	}

	public boolean getLoggedIn() {
		return loggedIn;
	}

	private Tree createFriendItem() {
		Tree friendItem = new Tree();

		friendItem.addItem(new TreeCommandItem("My Friends", MenuCommands.friends));
		friendItem.addItem(new TreeCommandItem("Search People", MenuCommands.friendssearch));
		friendItem.addItem(new TreeCommandItem("Twitter", MenuCommands.twitter));

		friendItem.addSelectionHandler(new SelectionHandler<TreeItem>() {
			@Override
			public void onSelection(SelectionEvent<TreeItem> event) {
				if (event.getSelectedItem() instanceof TreeCommandItem)
					((TreeCommandItem) event.getSelectedItem()).getCommand().execute();
			}
		});

		return friendItem;
	}

	private class TreeCommandItem extends TreeItem {
		private Command command;

		public TreeCommandItem(String text, Command command) {
			super();
			setText(text);
			setCommand(command);
		}

		public void setCommand(Command command) {
			this.command = command;
		}

		public Command getCommand() {
			return command;
		}
	}

	private static class MenuCommands {
		private static Command login = new Command() {
			@Override
			public void execute() {
				ViewController.getInstance().showLoginForm();
			}
		};
		private static Command logout = new Command() {
			@Override
			public void execute() {
				Util.getInstance().logout(new EmptyAsyncCallback());
				ViewController.getInstance().changeViewToLoggedOut();

			}
		};
		private static Command register = new Command() {
			@Override
			public void execute() {
				ViewController.getInstance().showRegisterForm();
			}
		};
		private static Command about = new Command() {
			@Override
			public void execute() {
				Window.alert("Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick\n"
						+ "  Licensed under the Apache License, Version 2.0 (the 'License');\n"
						+ "   you may not use this file except in compliance with the License.\n\n"
						+

						"You may obtain a copy of the License at\n\n"
						+

						"http://www.apache.org/licenses/LICENSE-2.0\n\n"
						+

						"Unless required by applicable law or agreed to in writing, software\n"
						+ "distributed under the License is distributed on an 'AS IS' BASIS,\n"
						+ "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
						+ "See the License for the specific language governing permissions and\n"
						+ "limitations under the License.\n");
			}
		};
		private static Command friends = new Command() {
			@Override
			public void execute() {
				ViewController.getInstance().showFriendsHome();
			}
		};
		private static Command admin = new Command() {
			@Override
			public void execute() {
				ViewController.getInstance().showAdminHome();
			}
		};
		private static Command twitter = new Command() {
			@Override
			public void execute() {
				ViewController.getInstance().showFriendsTwitter();
			}
		};
		private static Command friendssearch = new Command() {
			@Override
			public void execute() {
				ViewController.getInstance().showFriendsSearch();
			}
		};
		private static Command home = new Command() {
			@Override
			public void execute() {
				ViewController.getInstance().showClientHome();
			}
		};
	}

}
